home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Include / mymalloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  5.9 KB  |  215 lines

  1. #ifndef Py_MYMALLOC_H
  2. #define Py_MYMALLOC_H
  3. /* Lowest-level memory allocation interface */
  4.  
  5. #ifdef macintosh
  6. #define ANY void
  7. #endif
  8.  
  9. #ifdef __STDC__
  10. #define ANY void
  11. #endif
  12.  
  13. #ifdef __TURBOC__
  14. #define ANY void
  15. #endif
  16.  
  17. #ifdef __GNUC__
  18. #define ANY void
  19. #endif
  20.  
  21. #ifndef ANY
  22. #define ANY char
  23. #endif
  24.  
  25. #ifdef HAVE_STDLIB_H
  26. #include <stdlib.h>
  27. #endif
  28.  
  29. #include "myproto.h"
  30.  
  31. #ifdef __cplusplus
  32. /* Move this down here since some C++ #include's don't like to be included
  33.    inside an extern "C" */
  34. extern "C" {
  35. #endif
  36.  
  37. #ifdef SYMANTEC__CFM68K__
  38. #pragma lib_export on
  39. #endif
  40.  
  41. #ifndef DL_IMPORT       /* declarations for DLL import */
  42. #define DL_IMPORT(RTYPE) RTYPE
  43. #endif
  44.  
  45. #ifndef NULL
  46. #define NULL ((ANY *)0)
  47. #endif
  48.  
  49. #ifdef MALLOC_ZERO_RETURNS_NULL
  50. /* XXX Always allocate one extra byte, since some malloc's return NULL
  51.    XXX for malloc(0) or realloc(p, 0). */
  52. #define _PyMem_EXTRA 1
  53. #else
  54. #define _PyMem_EXTRA 0
  55. #endif
  56.  
  57. /*
  58.  * Core memory allocator
  59.  * =====================
  60.  */
  61.  
  62. /* To make sure the interpreter is user-malloc friendly, all memory
  63.    APIs are implemented on top of this one.
  64.  
  65.    The PyCore_* macros can be defined to make the interpreter use a
  66.    custom allocator. Note that they are for internal use only. Both
  67.    the core and extension modules should use the PyMem_* API.
  68.  
  69.    See the comment block at the end of this file for two scenarios
  70.    showing how to use this to use a different allocator. */
  71.  
  72. #ifndef PyCore_MALLOC_FUNC
  73. #undef PyCore_REALLOC_FUNC
  74. #undef PyCore_FREE_FUNC
  75. #define PyCore_MALLOC_FUNC      malloc
  76. #define PyCore_REALLOC_FUNC     realloc
  77. #define PyCore_FREE_FUNC        free
  78. #endif
  79.  
  80. #ifndef PyCore_MALLOC_PROTO
  81. #undef PyCore_REALLOC_PROTO
  82. #undef PyCore_FREE_PROTO
  83. #define PyCore_MALLOC_PROTO     Py_PROTO((size_t))
  84. #define PyCore_REALLOC_PROTO    Py_PROTO((ANY *, size_t))
  85. #define PyCore_FREE_PROTO       Py_PROTO((ANY *))
  86. #endif
  87.  
  88. #ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
  89. extern ANY *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
  90. extern ANY *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
  91. extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
  92. #endif
  93.  
  94. #ifndef PyCore_MALLOC
  95. #undef PyCore_REALLOC
  96. #undef PyCore_FREE
  97. #define PyCore_MALLOC(n)        PyCore_MALLOC_FUNC(n)
  98. #define PyCore_REALLOC(p, n)    PyCore_REALLOC_FUNC((p), (n))
  99. #define PyCore_FREE(p)          PyCore_FREE_FUNC(p)
  100. #endif
  101.  
  102. /* BEWARE:
  103.  
  104.    Each interface exports both functions and macros. Extension modules
  105.    should normally use the functions for ensuring binary compatibility
  106.    of the user's code across Python versions. Subsequently, if the
  107.    Python runtime switches to its own malloc (different from standard
  108.    malloc), no recompilation is required for the extensions.
  109.  
  110.    The macro versions trade compatibility for speed. They can be used
  111.    whenever there is a performance problem, but their use implies
  112.    recompilation of the code for each new Python release. The Python
  113.    core uses the macros because it *is* compiled on every upgrade.
  114.    This might not be the case with 3rd party extensions in a custom
  115.    setup (for example, a customer does not always have access to the
  116.    source of 3rd party deliverables). You have been warned! */
  117.  
  118. /*
  119.  * Raw memory interface
  120.  * ====================
  121.  */
  122.  
  123. /* Functions */
  124.  
  125. /* Function wrappers around PyCore_MALLOC and friends; useful if you
  126.    need to be sure that you are using the same memory allocator as
  127.    Python.  Note that the wrappers make sure that allocating 0 bytes
  128.    returns a non-NULL pointer, even if the underlying malloc
  129.    doesn't. Returned pointers must be checked for NULL explicitly.
  130.    No action is performed on failure. */
  131. extern DL_IMPORT(ANY *) PyMem_Malloc Py_PROTO((size_t));
  132. extern DL_IMPORT(ANY *) PyMem_Realloc Py_PROTO((ANY *, size_t));
  133. extern DL_IMPORT(void) PyMem_Free Py_PROTO((ANY *));
  134.  
  135. /* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
  136.    no longer supported. They used to call PyErr_NoMemory() on failure. */
  137.  
  138. /* Macros */
  139. #define PyMem_MALLOC(n)         PyCore_MALLOC(n)
  140. #define PyMem_REALLOC(p, n)     PyCore_REALLOC((ANY *)(p), (n))
  141. #define PyMem_FREE(p)           PyCore_FREE((ANY *)(p))
  142.  
  143. /*
  144.  * Type-oriented memory interface
  145.  * ==============================
  146.  */
  147.  
  148. /* Functions */
  149. #define PyMem_New(type, n) \
  150.     ( (type *) PyMem_Malloc((n) * sizeof(type)) )
  151. #define PyMem_Resize(p, type, n) \
  152.     ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
  153. #define PyMem_Del(p) PyMem_Free(p)
  154.  
  155. /* Macros */
  156. #define PyMem_NEW(type, n) \
  157.     ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
  158. #define PyMem_RESIZE(p, type, n) \
  159.     if ((p) == NULL) \
  160.         (p) = (type *)(PyMem_MALLOC( \
  161.                     _PyMem_EXTRA + (n) * sizeof(type))); \
  162.     else \
  163.         (p) = (type *)(PyMem_REALLOC((p), \
  164.                     _PyMem_EXTRA + (n) * sizeof(type)))
  165. #define PyMem_DEL(p) PyMem_FREE(p)
  166.  
  167. /* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
  168.    it is recommended to write the test explicitly in the code.
  169.    Note that according to ANSI C, free(NULL) has no effect. */
  170.  
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174.  
  175. /* SCENARIOS
  176.  
  177.    Here are two scenarios by Vladimir Marangozov (the author of the
  178.    memory allocation redesign).
  179.  
  180.    1) Scenario A
  181.  
  182.    Suppose you want to use a debugging malloc library that collects info on
  183.    where the malloc calls originate from. Assume the interface is:
  184.  
  185.    d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
  186.  
  187.    In this case, you would define (for example in config.h) :
  188.  
  189.    #define PyCore_MALLOC_FUNC      d_malloc
  190.    ...
  191.    #define PyCore_MALLOC_PROTO    Py_PROTO((size_t, char *, unsigned long))
  192.    ...
  193.    #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
  194.  
  195.    #define PyCore_MALLOC(n)    PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
  196.    ...
  197.  
  198.    2) Scenario B
  199.  
  200.    Suppose you want to use malloc hooks (defined & initialized in a 3rd party
  201.    malloc library) instead of malloc functions.  In this case, you would
  202.    define:
  203.  
  204.    #define PyCore_MALLOC_FUNC    (*malloc_hook)
  205.    ...
  206.    #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
  207.  
  208.    and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
  209.  
  210.  
  211. */
  212.  
  213.  
  214. #endif /* !Py_MYMALLOC_H */
  215.